'use strict';
/**
 * Created by zhouzhongyuan on 2016/1/6.
 */
$app.controller(function ($, _, then, backboneValidation, notice) {
  var _actions = {};
  var _objs    = {};
  /********************/
  _objs.login = {};

  _actions.index = function (params) {
    console.log(params);
    $("body").html("<a href='#auth/login'>login</a>")
  };

  _actions.login = function () {
    $app.require(["home/model/user-login", "home/view/auth/login"], function ($model, $view) {
      var view    = new $view;
      var model   = new $model;
      model.on("validated:valid", function () {
        notice.formInlineMsg([
          [view.$el.find("input[name='password']"), "验证成功"]
        ], true, function () {
          view.$el.find("em.state-error").remove();
          view.$el.find(".state-error").removeClass("state-error");
        });
        setTimeout(function () {
          $app.route().nav($app.route().to(["auth/index", {id: 1, p: 3}]));
        }, 500);
      });
      view.submit = function (data) {
        model.set(data);
        var errors  = model.validate();
        var _errors = [];
        _.each(errors, function (value, index) {
          //noinspection JSValidateTypes
          _errors.push([view.$el.find("input[name='" + index + "']").parent("label"), value]);
        });
        notice.formInlineMsg(_errors, false, function () {
          view.$el.find("em.state-error").remove();
          view.$el.find(".state-error").removeClass("state-error");
        });
      };
      view.render();
    });

  };
  return _actions;
});